var platforms=sessionStorage.getItem("platforms")
var editpicId=sessionStorage.getItem("editpicIds")

var listReturn=sessionStorage.getItem("listReturn")
var starss=1//保存后的状态会改变
var picIdall=null;//图片id用于保留当前状态
var edit_or_not=true//页面是否编辑
var listReturn2=true
//平台标签渲染
/*if(platforms==10){
    //签约
    $("#ordinary").removeAttr("class")
    $("#signing").attr("class","hover")

}else {
    //普通
    $("#signing").removeAttr("class")
    $("#ordinary").attr("class","hover")
}*/
// 获取地址栏参数  page为 1 是首页   page为2是编辑页面
if(getQueryString("page")){
    var page = getQueryString("page");
}
if(page == 2){
    //编辑页面
    renderingCarousel(platforms)
}else {
    //首页
    renderingCarousel(20)
}
//签约和普通切换的时候
function renderingCarousel2(val) {
    if(!edit_or_not){
        var curInd = layer.confirm(
            '<span>确认取消已编辑的模块？</span>'+'<br/>', {offset:'350px',title:'取消提示',btn: ['确定', '取消']}, function(index){
                edit_or_not = true;

                renderingCarousel(val)
                layer.close(curInd);
            },function(){
                layer.close(curInd);

            });
    }else {
        renderingCarousel(val)
    }


}

//渲染轮播图
function renderingCarousel(val) {
    if(page == 2){
        if(listReturn2==true){
            $("#editPages").css({
                display: "inline"
            });
        }else {
            $("#editPages").css({
                display: "none"
            });
        }

    }else {
        $("#editPages").css({
            display: "none"
        });
    }

    var datas={}

    var platformsall=""
    if(val==10){
        //签约
        $("#ordinary").removeAttr("class")
        $("#signing").attr("class","hover")
        platformsall=10

    }else {
        //普通
        $("#signing").removeAttr("class")
        $("#ordinary").attr("class","hover")
        platformsall=20
    }
    if(page == 2){
        datas={
            "pageNum":1,
            "pageSize":50,
            "platform": platformsall,//平台
        }
    }else {
        datas={
            "pageNum":1,
            "pageSize":50,
            "platform": platformsall,//平台
            "picStatus":10
        }
    }
    $.ajax({
        url: root.appversioncontrol.listWheelPictureBack,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify(datas),
        success: function (data) {

            var rotationChart = '';
            if(data.data!=null&&data.data!=""){

                if(data.data.total<Number(50)){
                    $("#newPictures").css({
                        display: "inline"
                    });
                }else {
                    $("#newPictures").css({
                        display: "none"
                    });
                }
                    //首页无图片时
                    if(editpicId==undefined){
                        for (var k = 0; k < data.data.data.length; k++){
                            rotationChart += '<li data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                            rotationChart += '</li>';
                        }
                        $("#viewAlls").css({
                            display: "block"
                        });
                    }else {
                        //starss等于2时是点击保存时图片id进行比较相同就选中状态
                        //有图片时
                        for (var k = 0; k < data.data.data.length; k++){
                            if(page == 2){
                                $("#viewAlls").css({
                                    display: "none"
                                });
                                //列表编辑进入时
                                if (starss==1) {
                                    if(data.data.data[k].platform!=0){
                                        //从首页进来图片和平台一致自动选中
                                        if(editpicId==data.data.data[k].picId && platforms==data.data.data[k].platform){
                                            rotationChart += '<li  data-id="'+data.data.data[k].picId+'" class="hover" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                            selectPictures(123456,data.data.data[k].picId)
                                            $("#editPages").css({
                                                display: "inline"
                                            });
                                        }else {
                                            rotationChart += '<li  data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                        }
                                    }else {
                                        //platform等于0时两个平台都有相同的图片，判断是从哪个平台点击过来进行渲染，选中
                                        if(editpicId==data.data.data[k].picId && platforms==val){
                                            rotationChart += '<li class="hover"  data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                            selectPictures(123456,data.data.data[k].picId)
                                        }else {
                                            rotationChart += '<li  data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                        }
                                    }
                                }else {
                                    if(data.data.data[k].platform!=0){
                                        //从首页进来图片和平台一致自动选中
                                        if(picIdall==data.data.data[k].picId ){
                                            rotationChart += '<li class="hover"  data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                            selectPictures(123456,data.data.data[k].picId)
                                            $("#editPages").css({
                                                display: "inline"
                                            });
                                        }else {
                                            rotationChart += '<li  data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                        }
                                    }else {
                                        //platform等于0时两个平台都有相同的图片，判断是从哪个平台点击过来进行渲染，选中
                                        if(picIdall==data.data.data[k].picId ){
                                            rotationChart += '<li class="hover"  data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';
                                            selectPictures(123456,data.data.data[k].picId)
                                            $("#editPages").css({
                                                display: "inline"
                                            });
                                        }else {
                                            rotationChart += '<li data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                            rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                            rotationChart += '</li>';

                                        }
                                    }
                                }


                            }else {
                                $("#viewAlls").css({
                                    display: "block"
                                });
                                //首页进入时
                                    if (starss==1) {
                                        rotationChart += '<li data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                        rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                        rotationChart += '</li>';
                                    }else {
                                        if(data.data.data[k].platform!=0){
                                            //从首页进来图片和平台一致自动选中
                                            if(picIdall==data.data.data[k].picId ){
                                                rotationChart += '<li class="hover" data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                                rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                                rotationChart += '</li>';
                                                selectPictures(123456,data.data.data[k].picId)
                                                $("#editPages").css({
                                                    display: "inline"
                                                });
                                            }else {
                                                rotationChart += '<li data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                                rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                                rotationChart += '</li>';
                                            }
                                        }else {
                                            //platform等于0时两个平台都有相同的图片，判断是从哪个平台点击过来进行渲染，选中
                                            if(picIdall==data.data.data[k].picId ){
                                                rotationChart += '<li class="hover" data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                                rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                                rotationChart += '</li>';
                                                selectPictures(123456,data.data.data[k].picId)
                                                $("#editPages").css({
                                                    display: "inline"
                                                });
                                            }else {
                                                rotationChart += '<li data-id="'+data.data.data[k].picId+'" onclick="selectPictures(this,'+data.data.data[k].picId+')">';
                                                rotationChart+='<img src="' + data.data.data[k].picUrl + '">'
                                                rotationChart += '</li>';

                                            }
                                        }
                                    }


                            }



                        }
                    }


            }else {
                $("#newPictures").css({
                    display: "inline"
                });
            }
            $('#rotationPicture').html(rotationChart);
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

//新增空白轮播图
var coner=50
function newPhotos() {
    if($('#rotationPicture li').length<coner){
        $.Huimodalalert('添加成功！',2000);
        var rotationChart = '';
        rotationChart += '<li data-id="" onclick="selectPictures(this)">';
        rotationChart+='<img  src="../../../img/blank.png">'
        rotationChart += '</li>';
        $('#rotationPicture').append(rotationChart);

    }else {

        $("#newPictures").css({
            display: "none"
        });

    }

}
//编辑
function selectPictures(val,picIds) {
    //切换轮播图编辑和保存的验证
        var curChoose = $('#rotationPicture').find('.hover');
        if(!edit_or_not){
            var curInd = layer.confirm(
                '<span>确认取消已编辑的模块？</span>'+'<br/>', {offset:'350px',title:'取消提示',btn: ['确定', '取消']}, function(index){
                    edit_or_not = true;                  
                    
                    changeCurImg(val,picIds);
                    layer.close(curInd);
                },function(){
                    layer.close(curInd);

                });
        }else{
            if(curChoose.length>0){
                curChooseObj = curChoose.attr('data-id');
                if(!curChooseObj){
                    $.Huimodalalert('轮播图信息为空，保留失败',2000);
                }

            }           

            changeCurImg(val,picIds);

        }



};

function  changeCurImg(val,picIds) {
    picIdall=picIds
    //图片选中效果
    $(val).addClass('hover');
    $(val).siblings().removeClass('hover')
    $("#editPages").css({
        display: "inline"
    });
    if(picIds!=undefined){
        $.ajax({
            url: root.appversioncontrol.getWheelPictureDetail,
            cache: false,  //禁用缓存
            type: "POST",
            dataType: 'json',
            data: {
                "picId":picIds,
            },

            success: function (data) {
                if(data.errorCode==0){
                    $("#picId").val(data.data.picId)
                    $("#picTitle").val(data.data.picTitle)
                    $("#picOrder").val(data.data.picOrder)
                    $("#createStartTimeValue").val(data.data.startTime)
                    $("#createEndTimeValue").val(data.data.endTime)
                    //区域
                    var saleAreaall=$("input:checkbox[name='saleArea']")
                    for(var k = 0; k < saleAreaall.length; k++) {
                        saleAreaall[k].checked = false;
                        for(var i=0;data.data.areaList.length>i;i++) {
                            if (data.data.areaList[i] == saleAreaall[k].value) {
                                saleAreaall[k].checked = true;
                            }
                        }
                    }
                    var saleArea=$("input:checkbox[name='saleArea']")
                    var saleArea2=$("input:checkbox[name='saleArea']:checked")
                    if(saleArea2.length==saleArea.length){
                        $("#saleAreas").prop("checked", true)
                    }else {
                        $("#saleAreas").prop("checked", false)
                    }
                    //平台选则
                    var platformSelectionall=$("input:checkbox[name='platformSelection']")
                    for(var j = 0; j < platformSelectionall.length; j++) {
                        platformSelectionall[j].checked = false;
                        if (data.data.platform==0){
                            platformSelectionall[j].checked = true;
                        }else {
                            if (data.data.platform == platformSelectionall[j].value) {
                                platformSelectionall[j].checked = true;
                            }
                        }

                    }

                    var platformSelection=$("input:checkbox[name='platformSelection']")
                    var platformSelection2=$("input:checkbox[name='platformSelection']:checked")
                    if(platformSelection2.length==platformSelection.length){
                        $("#platformSelections").prop("checked", true)

                        //平台2
                        $("#platformSelectionall2").css({
                            display: "inline"
                        });
                        $("#price_typeall3").css({
                            display: "inline"
                        });
                        var platformSelectionall2=$("input:checkbox[name='platformSelection2']")
                        for(var h = 0; h < platformSelectionall2.length; h++) {
                            platformSelectionall2[h].checked = false;
                            if(data.data.isRetailCustomer==0){
                                platformSelectionall2[h].checked = true;
                            }else {
                                if (data.data.isRetailCustomer == platformSelectionall2[h].value) {
                                    platformSelectionall2[h].checked = true;
                                }
                            }

                        }
                        var platformSelection=$("input:checkbox[name='platformSelection2']")
                        var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
                        if(platformSelection2.length==platformSelection.length){
                            $("#platformSelections2").prop("checked", true)
                        }else {
                            $("#platformSelections2").prop("checked", false)
                        }

                    }else {
                        $("#platformSelections").prop("checked", false)
                        if(platformSelection2.val()==10){
                            //签约平台时平台选择显示
                            //平台2
                            $("#platformSelectionall2").css({
                                display: "inline"
                            });
                            $("#price_typeall3").css({
                                display: "inline"
                            });
                            var platformSelectionall2=$("input:checkbox[name='platformSelection2']")
                            for(var h = 0; h < platformSelectionall2.length; h++) {
                                platformSelectionall2[h].checked = false;
                                if(data.data.isRetailCustomer==0){
                                    platformSelectionall2[h].checked = true;
                                }else {
                                    if (data.data.isRetailCustomer == platformSelectionall2[h].value) {
                                        platformSelectionall2[h].checked = true;
                                    }
                                }
                            }
                            var platformSelection=$("input:checkbox[name='platformSelection2']")
                            var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
                            if(platformSelection2.length==platformSelection.length){
                                $("#platformSelections2").prop("checked", true)
                            }else {
                                $("#platformSelections2").prop("checked", false)
                            }
                        }else {
                            $("#platformSelectionall2").css({
                                display: "none"
                            });
                            $("#price_typeall3").css({
                                display: "none"
                            });
                        }
                    }
                    $("#price_type1").prop('checked',false);
                    $("#price_type2").prop('checked',false);
                    $("#price_type3").prop('checked',false);
                    var price_typeall= data.data.priceType.split(",")
                    if(data.data.priceType!=""&&data.data.priceType!=null){
                        for (var s=0;s<price_typeall.length;s++) {
                            if(price_typeall[s]==10){
                                $("#price_type1").prop('checked',true);
                            }else if(price_typeall[s]==20){
                                $("#price_type2").prop('checked',true);
                            }if(price_typeall[s]==30){
                                $("#price_type3").prop('checked',true);
                            }
                        }
                    }else {
                        $("#price_type1").prop('checked',false);
                        $("#price_type2").prop('checked',false);
                        $("#price_type3").prop('checked',false);
                    }
                    pricetypeall()

                if(data.data.picUrl!=""&&data.data.picUrl!=null){
                    $('#fileupones').html('更换图片')
                    $("#myImg").attr("src",data.data.picUrl)
                }else {
                    $('#fileupones').html('图片上传')
                }

                    /*$("#specialLink").val(data.data.placeType)*/
                    console.log(data.data.placeContent)
                    spcialLinkConfit.specialLinkArr[0].specialInd = data.data.placeType;
                    spcialLinkConfit.specialLinkArr[0].specialContent = data.data.placeContent;
                    if(data.data.detailContent!=null){
                        spcialLinkConfit.specialLinkArr[0].detailContent = data.data.detailContent;
                    }else {
                        spcialLinkConfit.specialLinkArr[0].detailContent = "";
                    }

                    spcialLinkConfit.specialDefaultInit();
                    //内容
                    $(".specialLink").attr('dataId',picIds);                    
                   
                }else {
                    $.Huimodalalert(data.msg,2000);
                }
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
    }else {
        $("#price_type1").prop('checked',false);
        $("#price_type2").prop('checked',false);
        $("#price_type3").prop('checked',false);
        $('#fileupones').html('图片上传')
        $("#picId").val("")
        $("#picTitle").val("")
        $("#picOrder").val("")
        $("#createStartTimeValue").val("")
        $("#createEndTimeValue").val("")
        //区域
        var saleAreaall=$("input:checkbox[name='saleArea']")
        for(var k = 0; k < saleAreaall.length; k++) {
            saleAreaall[k].checked = false;
            saleAreaall[k].checked = true;
        }
        var saleArea=$("input:checkbox[name='saleArea']")
        var saleArea2=$("input:checkbox[name='saleArea']:checked")
        if(saleArea2.length==saleArea.length){
            $("#saleAreas").prop("checked", true)
        }else {
            $("#saleAreas").prop("checked", false)
        }
        //平台选则
        var platformSelectionall=$("input:checkbox[name='platformSelection']")
        for(var j = 0; j < platformSelectionall.length; j++) {
            platformSelectionall[j].checked = false;
            platformSelectionall[j].checked = true;

        }

        var platformSelection=$("input:checkbox[name='platformSelection']")
        var platformSelection2=$("input:checkbox[name='platformSelection']:checked")
        if(platformSelection2.length==platformSelection.length){
            $("#platformSelections").prop("checked", true)

            //平台2
            $("#platformSelectionall2").css({
                display: "inline"
            });
            $("#price_typeall3").css({
                display: "inline"
            });
            var platformSelectionall2=$("input:checkbox[name='platformSelection2']")
            for(var h = 0; h < platformSelectionall2.length; h++) {
                platformSelectionall2[h].checked = false;
                platformSelectionall2[h].checked = true;

            }
            var platformSelection=$("input:checkbox[name='platformSelection2']")
            var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
            if(platformSelection2.length==platformSelection.length){
                $("#platformSelections2").prop("checked", true)
            }else {
                $("#platformSelections2").prop("checked", false)
            }

        }else {
            $("#platformSelections").prop("checked", false)
            if(platformSelection2.val()==10){
                //签约平台时平台选择显示
                //平台2
                $("#platformSelectionall2").css({
                    display: "inline"
                });
                $("#price_typeall3").css({
                    display: "inline"
                });
                var platformSelectionall2=$("input:checkbox[name='platformSelection2']")
                for(var h = 0; h < platformSelectionall2.length; h++) {
                    platformSelectionall2[h].checked = false;
                    platformSelectionall2[h].checked = true;

                }
                var platformSelection=$("input:checkbox[name='platformSelection2']")
                var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
                if(platformSelection2.length==platformSelection.length){
                    $("#platformSelections2").prop("checked", true)
                }else {
                    $("#platformSelections2").prop("checked", false)
                }
            }else {
                $("#platformSelectionall2").css({
                    display: "none"
                });
                $("#price_typeall3").css({
                    display: "none"
                });
            }


        }
    }
    $("#myImg").attr("src","")
    $("#specialLink").val(-1)
    spcialLinkConfit.specialLinkArr[0].specialInd = "-1";
    spcialLinkConfit.specialLinkArr[0].specialContent = "";
    spcialLinkConfit.specialLinkArr[0].detailContent = "";
    $(".specialLink").attr('dataId',"")
    spcialLinkConfit.specialDefaultInit()


}

//图片上传验证
var formData = new FormData();
// 提交上传图片
function getupfilerul(can,e){
    var formData = new FormData();
    formData.append('file',$(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            for(var i=0; i<data.length; i++){
                $(can).parent().parent().parent().find('img').attr("src", data[0].fullPath);
                $(can).parent().find('.fileupone').html('更换图片')
                $("#fileUpone").attr('type','text');
                $("#fileUpone").attr('type','file');
            }

        }
    })
}
// 上传图片返现
function mobanputDataToPage(e,can) {
    edit_or_not=false
    //验证
    verificationPicFileType(can)
    verificationPicFileSize(can)
    for(var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if(!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src= data;
            img.onload = function(){
                var width = img.width;
                var height = img.height;
                console.log(height)
                if (Number(width) == Number(686) && Number(height) == Number(280)){
                    getupfilerul(can,e)

                }else {
                    $.Huimodalalert('图片要求686px*280px像素，请重新上传 ',2000);
                    file.value = "";
                    $("#fileUpone").attr('type','text');
                    $("#fileUpone").attr('type','file');
                    return;

                }

            };


        }
    }
    formData.append('file', $(can)[0].files[0]);
}
// 图片类型验证
function verificationPicFileType(file) {
    var flag=false;
    var fileTypes = [".jpg", ".png",".gif",".jpeg"];
    var filePath = file.value;
    //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
    if(filePath){
        var isNext = false;
        var fileEnd = filePath.substring(filePath.indexOf("."));
        for (var i = 0; i < fileTypes.length; i++) {
            if (fileTypes[i] == fileEnd) {
                isNext = true;
                flag= true;
                break;
            }
        }
        if (!isNext){
            $.Huimodalalert('格式仅支持jpg、jpeg、gif、png',2000);
            file.value = "";
            flag=false;
        }
    }
    return flag;
}
//图片大小验证
function verificationPicFileSize(file) {
    var flag=false;
    var fileSize = 0;
    var fileMaxSize = 300;
    var filePath = file.value;
    if(filePath){
        fileSize =file.files[0].size;
        var size = fileMaxSize * Number(1024);
        if (fileSize > size) {
            $.Huimodalalert('图片大小超出300KB',2000);
            file.value = "";
            flag=false;
        }else{
            flag=true;
        }
    }
    return flag;
}
//删除图片
function deletfilefns(e){
    edit_or_not=false
    $('#fileupones').html('图片上传')
    $(e).parent().parent().parent().find('img').attr('src','')
    $("#fileUpone").attr('type','text');
    $("#fileUpone").attr('type','file');

}


//保存
$("#form-member").validate({
    rules: {
        picTitle:{
            required: true,//是否是必填项
        },
        picOrder:{
            required: true,//是否是必填项
        },
        createStartTimeValue:{
            required: true,//是否是必填项
        },
        createEndTimeValue:{
            required: true,//是否是必填项
        },
    },
    //提交表单数据
    submitHandler:function(form){
        console.log(spcialLinkConfit.specialLinkArr[0])
        var areaLists=[]
        for(var i=0;$("input:checkbox[name='saleArea']:checked").length>i;i++) {
            areaLists.push($("input:checkbox[name='saleArea']:checked")[i].value)
        }
        if(areaLists==""){
            $.Huimodalalert('请选择区域',2000);
            return false;
        }
     if($("input:checkbox[name='platformSelection']:checked").val()==undefined){
         $.Huimodalalert('请选择平台',2000);
         return false;
     }

        //两个平台选择联动判断
        //平台1
        var platforms=""
        if($("#platformSelections").is(':checked')==true){
            platforms=0
        }else {

            platforms=$("input:checkbox[name='platformSelection']:checked").val()
        }
        //平台2
        var isRetailCustomers=""
        if($("#platformSelections").is(':checked')==true && $("#platformSelections2").is(':checked')==true){
            isRetailCustomers=0
        }else {

            if($("input:checkbox[name='platformSelection']:checked").val()==10){
                if($("#platformSelections2").is(':checked')==true){
                    isRetailCustomers=0
                }else {
                    if($("input:checkbox[name='platformSelection2']:checked").val()==undefined){
                        $.Huimodalalert('请选择平台',2000);
                        return false;
                    }
                    isRetailCustomers=$("input:checkbox[name='platformSelection2']:checked").val()
                }
            }else {
                if($("#platformSelections").is(':checked')==true){
                    if($("#platformSelections2").is(':checked')==true){
                        isRetailCustomers=0
                    }else {
                        if($("input:checkbox[name='platformSelection2']:checked").val()==undefined){
                            $.Huimodalalert('请选择平台',2000);
                            return false;
                        }
                        isRetailCustomers=$("input:checkbox[name='platformSelection2']:checked").val()
                    }
                }else {
                    if($("#platformSelections2").is(':checked')==true){
                        isRetailCustomers=0
                    }else {
                        if($("input:checkbox[name='platformSelection']:checked").val()==20){
                            isRetailCustomers=""

                        }else {
                            if($("input:checkbox[name='platformSelection2']:checked").val()==undefined){
                                $.Huimodalalert('请选择平台',2000);
                                return false;
                            }
                            isRetailCustomers=$("input:checkbox[name='platformSelection2']:checked").val()

                        }
                    }
                }

            }
        }
        var price_types = "";
        var price_typeall=[]
        for (var f = 0; f < $("input:checkbox[name='price_typeall']:checked").length; f++) {
            price_typeall.push($("input:checkbox[name='price_typeall']:checked")[f].value)
        }
        if(platforms==20){
            for (var h = 0; h < price_typeall.length; h++) {
                if (price_typeall[h] == 30) {
                    price_typeall.splice(h, 1);
                }
            }
        }
        for (var g = 0; g < price_typeall.length; g++) {
            price_types += price_typeall[g]+ ",";
        }
        //去掉最后一个逗号(如果不需要去掉，就不用写)
        if (price_types.length > 0) {
            price_types = price_types.substr(0, price_types.length - 1);
        }
        console.log(price_types)
        if(price_types==""){
            $.Huimodalalert('请选择价格类型',2000);
            return false;
        }

        if($("#myImg").attr("src")==""){
            $.Huimodalalert('请上传图片',2000);
            return false;
        }
        if(spcialLinkConfit.specialLinkArr[0].specialInd == 8){//无跳转
            if(spcialLinkConfit.specialLinkArr[0].specialInd ==''){
                $.Huimodalalert('请填写必填信息',2000);
                return
            }
        }else if( spcialLinkConfit.specialLinkArr[0].specialContent =='' &&  spcialLinkConfit.specialLinkArr[0].detailContent !=''){
            if(spcialLinkConfit.specialLinkArr[0].specialInd == 1){
                $.Huimodalalert('暂无该专题，请重新输入',2000);
                return
            }else if(spcialLinkConfit.specialLinkArr[0].specialInd == 3){
                $.Huimodalalert('暂无该品牌信息，请重新输入',2000);
                return
            }
        }else{
            if(spcialLinkConfit.specialLinkArr[0].specialInd =='' ||  spcialLinkConfit.specialLinkArr[0].specialContent ==''){
                $.Huimodalalert('请填写必填信息',2000);
                return
            }
        }
        if($("#picId").val()==""){
            //保存
            $.ajax({
                url: root.appversioncontrol.addWheelPicture,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data: JSON.stringify({
                    "picTitle":$("#picTitle").val(),
                    "picOrder":$("#picOrder").val(),
                    "areaList": areaLists,
                    "platform":platforms,
                    "isRetailCustomer": isRetailCustomers,
                    "priceType":price_types,
                    "startTime":$("#createStartTimeValue").val(),
                    "endTime":$("#createEndTimeValue").val(),
                    "picUrl":$("#myImg").attr("src"),
                    "placeType":spcialLinkConfit.specialLinkArr[0].specialInd,//插入链接类型
                    "placeContent":spcialLinkConfit.specialLinkArr[0].specialContent,//内容
                    "detailContent":spcialLinkConfit.specialLinkArr[0].detailContent,
                    "userName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
                }),
                success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert('保存成功',2000);
                        $(".specialLink").attr('dataId',data.data);

                        /*setTimeout(function () {
                            location.replace(location.href);
                            starss=2
                        }, 1000);*/
                        if(page == 2){
                            listReturn2=false
                            renderingCarousel(20)
                        }else {
                           renderingCarousel(20)

                        }


                        starss=2
                        edit_or_not=true
                    }else if(data.errorCode==4){
                        var names=""
                            var msgs=data.msg
                            msgs = msgs.match(/(\S*)区域/)[1];
                        var region=JSON.parse(sessionStorage.getItem("warehouses")).data
                        for (var j = 0; j < region.length; j++) {
                            if(msgs==region[j].warehouseCode){
                                names=region[j].warehouseName
                                names=names.match(/(\S*)仓/)[1];
                            }
                        }
                        $.Huimodalalert(names+'区域生效轮播图数量超出10个，请重新编辑',2000);
                    }else {
                        $.Huimodalalert(data.msg,2000);
                    }
                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            });
        }else {
            //修改
            $.ajax({
                url: root.appversioncontrol.updateWheelPicture,
                cache: false,  //禁用缓存
                type: "POST",
                dataType:'json',
                contentType: "application/json",
                data: JSON.stringify({
                    "picId":$("#picId").val(),
                    "picTitle":$("#picTitle").val(),
                    "picOrder":$("#picOrder").val(),
                    "areaList": areaLists,
                    "platform":platforms,
                    "priceType":price_types,
                    "isRetailCustomer": isRetailCustomers,
                    "startTime":$("#createStartTimeValue").val(),
                    "endTime":$("#createEndTimeValue").val(),
                    "picUrl":$("#myImg").attr("src"),
                    "placeType":spcialLinkConfit.specialLinkArr[0].specialInd,//插入链接类型
                    "placeContent":spcialLinkConfit.specialLinkArr[0].specialContent,//内容
                    "detailContent":spcialLinkConfit.specialLinkArr[0].detailContent,
                    "userName":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
                }),
                success: function (data) {
                    if(data.errorCode==0){
                        $.Huimodalalert('保存成功',2000);
                        if(spcialLinkConfit.specialLinkArr[0].specialInd == 6){
                              $(".specialLink").attr('dataId',$("#picId").val());
                          }
                       if(platforms==0){
                            renderingCarousel(20)
                       }else {
                             renderingCarousel(platforms)
                         }
                       starss=2
                      edit_or_not=true
                    }else if(data.errorCode==4){
                        var names=""
                        var msgs=data.msg
                        msgs = msgs.match(/(\S*)区域/)[1];
                        var region=JSON.parse(sessionStorage.getItem("warehouses")).data
                        for (var j = 0; j < region.length; j++) {
                            if(msgs==region[j].warehouseCode){
                                names=region[j].warehouseName
                                names=names.match(/(\S*)仓/)[1];
                            }
                        }
                        $.Huimodalalert(names+'区域生效轮播图数量超出10个，请重新编辑',2000);
                    }else {
                        $.Huimodalalert(data.msg,2000);
                    }
                },
                error: function () {
                    $.Huimodalalert('失败！',2000);
                }
            });
        }


    }
})
//取消
function cancelModule() {
    console.log($("#picId").val())
    var platformss=""
    if($("#platformSelections").is(':checked')==true){
        platformss=0
    }else {

        platformss=$("input:checkbox[name='platformSelection']:checked").val()
    }
    var curInd = layer.confirm(
        '<span>确认取消已编辑的模块？</span>'+'<br/>', {offset:'350px',title:'取消提示',btn: ['确定', '取消']}, function(index){
            //列表编辑点击取消确认时需要返回上一页
            if(page == 2){
                parent.location.reload(); // 父页面刷新
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            }else {
                starss=2
                edit_or_not=true
                if($("#picId").val()==""){
                    cancelsall()
                }else {
                    if(platformss==0){
                        renderingCarousel(20)
                    }else {
                        renderingCarousel(platformss)
                    }
                }



            }

            layer.close(curInd);

        },function(){
            layer.close(curInd);

        });
}
//查看所有
function viewAll(title,url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url,
        end:function () {
            renderingCarousel(20)
        }
    });
    layer.full(index);
}
//限制字符
var len
var Preser
var common=20//要限制输入的字数
function checkMaxIut(me) {
    edit_or_not=false
    var text=$(me).val()
    strlen(text)
    if(len>common){
        $(me).val(Preser)
    }else {
        Preser=$(me).val()
    }
}
function strlen(str){
    len = 0;
    for (var i=0; i<str.length; i++) {
        var c = str.charCodeAt(i);
        //单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len++;
        } else {
            len+=2;
        }
    }
    return len;
}
//验证只能输入数字，不能输入0
function Verificationall(val) {
    edit_or_not=false
    val.value = val.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
    val.value = val.value.replace(/^\./g,""); //验证第一个字符是数字
    val.value = val.value.replace(/\.{2,}/g,""); //只保留第一个, 清除多余的
    val.value = val.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    val.value=val.value.replace(/^[0]+[0-9]*$/gi,"")
}
//编辑中
function editings() {
    edit_or_not=false
}
//落地页配置
specialInd = 0;
specialContent="";
$('#specialLink').on('change',function(){
    edit_or_not=false
    specialInd = $(this).val();
    console.log(specialInd)
    if(specialInd !=7 && specialInd !=-1){
        $('#specContent').load('../special/special_link.html');
    }else{
        $('#specContent').html('');
    }
})
//区域编辑了
$(function(){
    $("input:checkbox[name='saleArea']").click(function() {
        edit_or_not=false
    });
})
//平台选择1编辑了
$(function(){
    $("input:checkbox[name='platformSelection']").click(function() {
        edit_or_not=false
    });
})
//平台选择2编辑了
$(function(){
    $("input:checkbox[name='platformSelection2']").click(function() {
        edit_or_not=false
    });
})
//区域选择全选全不选
function goodselection() {
    edit_or_not=false
    if($("#saleAreas").is(':checked')==true){
        var saleArea=$("input:checkbox[name='saleArea']")
        for(var i = 0; i < saleArea.length; i++) {
            saleArea[i].checked = true;
        }
    }else {
        var saleArea=$("input:checkbox[name='saleArea']")
        for(var i = 0; i < saleArea.length; i++) {
            saleArea[i].checked = false;
        }
    }

}
//子级选中
goodselectionall()
function goodselectionall() {
    var saleArea=$("input:checkbox[name='saleArea']")
    var saleArea2=$("input:checkbox[name='saleArea']:checked")
    if(saleArea2.length==saleArea.length){
        $("#saleAreas").prop("checked", true)
    }else {
        $("#saleAreas").prop("checked", false)
    }
}
//平台选择全选全不选
function platformSelectionalls() {
    edit_or_not=false
    if($("#platformSelections").is(':checked')==true){
        var platformSelection=$("input:checkbox[name='platformSelection']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = true;
            $("#platformSelectionall2").css({
                display: "inline"
            });
            $("#price_typeall3").css({
                display: "inline"
            });
        }
    }else {
        var platformSelection=$("input:checkbox[name='platformSelection']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = false;
            $("#platformSelectionall2").css({
                display: "none"
            });
            $("#price_typeall3").css({
                display: "none"
            });
        }
    }
}
platformSelectionall()
function platformSelectionall() {
    var platformSelection=$("input:checkbox[name='platformSelection']")
    var platformSelection2=$("input:checkbox[name='platformSelection']:checked")
    if(platformSelection2.length==platformSelection.length){
        $("#platformSelections").prop("checked", true)
        $("#platformSelectionall2").css({
            display: "inline"
        });
        $("#price_typeall3").css({
            display: "inline"
        });

    }else {
        $("#platformSelections").prop("checked", false)
        if(platformSelection2.val()==10){
            //签约平台时平台选择显示
            $("#platformSelectionall2").css({
                display: "inline"
            });
            $("#price_typeall3").css({
                display: "inline"
            });
        }else {
            $("#platformSelectionall2").css({
                display: "none"
            });
            $("#price_typeall3").css({
                display: "none"
            });
        }
    }
}
//平台2
function platformSelectionalls2() {
    edit_or_not=false
    if($("#platformSelections2").is(':checked')==true){
        var platformSelection=$("input:checkbox[name='platformSelection2']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = true;
        }
    }else {
        var platformSelection=$("input:checkbox[name='platformSelection2']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = false;
        }
    }
}
platformSelectionall2()
function platformSelectionall2() {
    var platformSelection=$("input:checkbox[name='platformSelection2']")
    var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
    if(platformSelection2.length==platformSelection.length){
        $("#platformSelections2").prop("checked", true)
    }else {
        $("#platformSelections2").prop("checked", false)
    }
}
//价格类型
function pricetypes() {
    edit_or_not=false
    if($("#price_types").is(':checked')==true){
        var platformSelection=$("input:checkbox[name='price_typeall']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = true;
        }
    }else {
        var platformSelection=$("input:checkbox[name='price_typeall']")
        for(var i = 0; i < platformSelection.length; i++) {
            platformSelection[i].checked = false;
        }
    }
}
pricetypeall()
function pricetypeall() {
    var platformSelection=$("input:checkbox[name='price_typeall']")
    var platformSelection2=$("input:checkbox[name='price_typeall']:checked")
    if(platformSelection2.length==platformSelection.length){
        $("#price_types").prop("checked", true)
    }else {
        $("#price_types").prop("checked", false)
    }
}
//新增时取消当前数据
function  cancelsall() {
    $("#picId").val("")
    $("#picTitle").val("")
    $("#picOrder").val("")
    $("#createStartTimeValue").val("")
    $("#createEndTimeValue").val("")
    //区域
    var saleAreaall=$("input:checkbox[name='saleArea']")
    for(var k = 0; k < saleAreaall.length; k++) {
        saleAreaall[k].checked = false;
        saleAreaall[k].checked = true;
    }
    var saleArea=$("input:checkbox[name='saleArea']")
    var saleArea2=$("input:checkbox[name='saleArea']:checked")
    if(saleArea2.length==saleArea.length){
        $("#saleAreas").prop("checked", true)
    }else {
        $("#saleAreas").prop("checked", false)
    }
    //平台选则
    var platformSelectionall=$("input:checkbox[name='platformSelection']")
    for(var j = 0; j < platformSelectionall.length; j++) {
        platformSelectionall[j].checked = false;
        platformSelectionall[j].checked = true;

    }

    var platformSelection=$("input:checkbox[name='platformSelection']")
    var platformSelection2=$("input:checkbox[name='platformSelection']:checked")
    if(platformSelection2.length==platformSelection.length){
        $("#platformSelections").prop("checked", true)

        //平台2
        $("#platformSelectionall2").css({
            display: "inline"
        });
        $("#price_typeall3").css({
            display: "inline"
        });
        var platformSelectionall2=$("input:checkbox[name='platformSelection2']")
        for(var h = 0; h < platformSelectionall2.length; h++) {
            platformSelectionall2[h].checked = false;
            platformSelectionall2[h].checked = true;

        }
        var platformSelection=$("input:checkbox[name='platformSelection2']")
        var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
        if(platformSelection2.length==platformSelection.length){
            $("#platformSelections2").prop("checked", true)
        }else {
            $("#platformSelections2").prop("checked", false)
        }

    }else {
        $("#platformSelections").prop("checked", false)
        if(platformSelection2.val()==10){
            //签约平台时平台选择显示
            //平台2
            $("#platformSelectionall2").css({
                display: "inline"
            });
            $("#price_typeall3").css({
                display: "inline"
            });
            var platformSelectionall2=$("input:checkbox[name='platformSelection2']")
            for(var h = 0; h < platformSelectionall2.length; h++) {
                platformSelectionall2[h].checked = false;
                platformSelectionall2[h].checked = true;

            }
            var platformSelection=$("input:checkbox[name='platformSelection2']")
            var platformSelection2=$("input:checkbox[name='platformSelection2']:checked")
            if(platformSelection2.length==platformSelection.length){
                $("#platformSelections2").prop("checked", true)
            }else {
                $("#platformSelections2").prop("checked", false)
            }
        }else {
            $("#platformSelectionall2").css({
                display: "none"
            });
            $("#price_typeall3").css({
                display: "none"
            });
        }
    }

$("#myImg").attr("src","")
$("#specialLink").val(-1)
spcialLinkConfit.specialLinkArr[0].specialInd = "-1";
spcialLinkConfit.specialLinkArr[0].specialContent = "";
spcialLinkConfit.specialLinkArr[0].detailContent = "";
$(".specialLink").attr('dataId',"")
spcialLinkConfit.specialDefaultInit()
}
function picShow(title, url) {
    var imgUrl=$("#myImg").attr("src")
    window.event ? window.event.cancelBubble = true : e.stopPropagation();
    sessionStorage.setItem("imgUrl", imgUrl);
    layer_show(title, url, "800", "500");
}